package com.example.foo.api.web.passport;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ad.api.utils.SmsSendUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.example.support.redis.RedisHelper;

@org.springframework.web.bind.annotation.RestController
public class VerificationCodeAction {

	public VerificationCodeAction() {
	}

	@PostMapping("/passport/verificationcodes")
	public AjaxResult createVerificationCode(@RequestParam("cellphone") String cellphone) {
		//
		if (StringUtils.isBlank(cellphone)) {
			return AjaxResult.error(400, "cellphone must not be null");
		}

		//
		String lockKey = String.format("ratelimit:verificationcode:cellphone_%s", cellphone);
		if (!RedisHelper.setnxex(lockKey, 59, "-")) {
			return AjaxResult.error(400, "请求频繁");
		}

		//
		SmsSendUtils.sendSmsCode(cellphone);

		return AjaxResult.success();
	}

}
